package com.jtyoui.service;

import com.jtyoui.model.InMessage;
import com.jtyoui.model.OutMessage;
import com.jtyoui.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class WebSocketService {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    public void sendOnlineUser(Map<String, User> onlineUser) {
        String msg = "";
        for (Map.Entry<String, User> entry : onlineUser.entrySet())
            msg = msg.concat(entry.getValue().getUsername() + "@");
        simpMessagingTemplate.convertAndSend("/topic/onlineUser", new OutMessage(msg));
    }

    public void sendTopicChat(InMessage inMessage) {
        String msg = inMessage.getFrom() + "@" + inMessage.getContent();
        simpMessagingTemplate.convertAndSend("/topic/chat", new OutMessage(msg));
    }

}
